<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Property;
use App\Models\Currency;
use Exception;
use DB;
use App\Library\Orders\Orders;

/**
 * 资产.
 */
class PropertyController extends Controller
{
    /**
     * 资产列表.
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/18
     * @param  int    uid    用户ID
     * @return json 资产列表
     */
    public function property_list(Request $request)
    {
        $data = [];
        $uid = $request->get('uid');
        if (empty($uid))
            return respF(1000);
        $order_model = new Orders();
        $id = $order_model->currency_id()->toArray();
        $id_string = implode(',', $id);
        $orderby_cid = 'field(cid,'.$id_string.')';
        $list = Property::
            where(['uid' => $uid])
            ->whereIn('cid', $id)
            ->orderByRaw($orderby_cid)
            ->get();
        $cny_price = UsdRate() ?? 6.3;
        $num = 0;
        if (count($list) > 0) {
            $all = array();
            //处理数据
            $usd_id = $order_model->get_standard();
            foreach ($list as $key => $val) {
                if ($val->cid == $usd_id) {
                    //$num = bcadd($val->total, $num, 2);
                } else {
                    $data[$key]['c_name'] = $val->currency->name_en; //币种名
                    $data[$key]['c_logo'] = config('alioss.host').'/'.$val->currency->logo; //币种logo
                    $data[$key]['cid'] = $val->cid; //币种id
                    $data[$key]['balance'] = $val->balance; //可用资产
                    $data[$key]['blocked'] = $val->blocked; //冻结资产
                    $data[$key]['total'] = $val->total; //总资产
                    $data[$key]['is_standard'] = $val->currency->is_standard; //总资产
                    $price = $order_model->currency_price($val->cid);
                    if (!$price)
                        return respF(1000);
//                    $num = $num + bcmul($price, $val->total, 2);
                    $num = bcmul($price, $val->total, 2);
                }
            }
        }
        $all['usd'] = bcadd($num, 0, 2); //这里小数点显示异常，采用bc函数处理
        $all['cny'] = bcmul($cny_price, $num, 2);
        $all['list'] = array_values($data);
        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $all]);
    }

    /**
     * 资产信息.
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/13
     * @param  int    uid    用户ID
     * @param  int    type   类型 1本位币  2交易货币
     * @param  int    cid    货币ID
     * @return json 资产信息
     */
    public function property_info(Request $request)
    {
        $info = $request->all();
        if (empty($info['uid']) || empty($info['cid']))
            return respF(1004);
        $order_model = new Orders();
        $currency = $order_model->currency_verify($info['cid']);
        if (!$currency)
            return respF(1004);
        $where['cid'] = $info['cid'];
        $where['uid'] = $info['uid'];
        $infos = Property::where($where)->first(['balance', 'blocked', 'total']);
        if (empty($infos))
            return response()->json(['status' => -2, 'msg' => '请添加资产']);
        $list = $infos->toArray();
        $list['logo'] = config('alioss.host').'/'.$currency->logo;
        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $list]);
    }

    /**
     * 资产信息(USDT).
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/13
     * @param  int    uid    用户ID
     * @param  int    type   类型 1本位币  2交易货币
     * @param  int    cid    货币ID
     * @return json 资产信息
     */
    public function property_my(Request $request)
    {
        $info = $request->all();
        if (empty($info['uid']))
            return respF(1004);
        $order_model = new Orders();
        $cid = $order_model->get_standard();
        $curr = $order_model->currency_verify($cid);
        $infos = $order_model->property_verif($info['uid'], $cid);
        $infos['name'] = $curr->name_en;
        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $infos]);
    }

    /**
     * 需要添加的货币列表.
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/18
     * @param  int    uid    用户ID
     * @return json
     */
    public function add_currency_list(Request $request)
    {
        $info = $request->all();
        if (empty($info['uid']))
            return respF(1004);
        $where['pay_switch'] = 1;
        $where['buy_switch'] = 1;
        $where1['uid'] = $info['uid'];
        //添加资产列表去掉USDT选项
        $list = Currency::where($where)->where('is_standard', '<>', 1)->get(['id', 'name_en', 'logo']);
        if (!$list)
            return respF(1004);
        $list = $list->toArray();
        foreach ($list as $k => $v) {
            $where1['cid'] = $v['id'];
            if (Property::where($where1)->first()) {
                unset($list[$k]);
            }else{
                $list[$k]['logo'] = config('alioss.host').'/'. $v['logo'];
            }
        }
        $data['type'] = 0;
        if (count($list) > 0) {
            $data['type'] = 1;
            $data['list'] = array_values($list);
        }
        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $data]);
    }

    /**
     * 资产添加.
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/17
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|bool|array comment
     */
    public function property_add(Request $request)
    {
        $info = $request->all();
        if (empty($info['uid']) || empty($info['cid']) || !is_array($info['cid']) || count($info['cid']) < 0)
            return respF(1004);
//        $where['uid'] = $info['uid'];
//        $user = Property::where($where)->first();
//        if ($user)
//            return respF(1042);
        $where['uid'] = $info['uid'];
        $cids = array_unique($info['cid']);
        foreach ($cids as $k => $v) {
            $where['cid'] = $v;
            $infos = Property::where($where)->first();
            if ($infos)
                return respF(1042);
            $data[$k]['uid'] = $info['uid'];
            $data[$k]['cid'] = $v;
        }
        try {
            //开启事务
            DB::beginTransaction();
            Property::insert($data);
            DB::commit();
            return response()->json(['status' => 1, 'msg' => '添加成功']);
        } catch (Exception $e) {
            DB::rollback();
            return respF(1000);
        }
    }

    /**
     * 可卖资产
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function sell_assets(Request $request)
    {
        $uid = $request->get('uid');
        $cid = Currency::where('name_en', 'USDT')->value('id');

        if (empty($uid) || empty($cid)) {
            return respF(1003);
        }

        $property = Property::where(['uid' => $uid, 'cid' => $cid])->first();

        if (collect($property)->count() == 0) {
            return respF(1005);
        }

        $data['balance'] = $property->balance;

        return response()->json(['status' => 1, 'msg' => '查询成功', 'data' => $data]);
    }
}
